#include "pointMotion.h"


//extern unsigned short pointNext[9];
unsigned short pointColisionFlag = 0;				// define colisions
unsigned int pointCNConditionFlag = 0;				// curent and next point condition
unsigned int pointMixedConditionFlag = 0;			// mixed next of the point whit curent of the other 2 points

//
//Sets the pointCNConditionFlag 
//
void pointConditionFlagsSet(void)
{
	register unsigned int 	localFlag = 0;
	
	register unsigned short p1Xn = pointsNext[0];
	register unsigned short p1Yn = pointsNext[1];
	register unsigned short p2Xn = pointsNext[3];
	register unsigned short p2Yn = pointsNext[4];
	register unsigned short p3Xn = pointsNext[6];
	register unsigned short p3Yn = pointsNext[7];
	
	register unsigned short p1X = points[0].x;
	register unsigned short p1Y = points[0].y;
	register unsigned short p2X = points[1].x;
	register unsigned short p2Y = points[1].y;
	register unsigned short p3X = points[2].x;
	register unsigned short p3Y = points[2].y;
	
	// Seting the pointCNConditionFlag
	
	// Condition of the curent point positions
	
	if( (p1X - p2X)==1 || (p2X - p1X)==1 ){			// bit 0 
		localFlag ^= 0x000001;
	}

	if( (p1X - p3X)==1 || (p3X - p1X)==1 ){			//bit 1
		localFlag ^= 0x000002;
	}
	
	if( (p3X - p2X)==1 || (p2X - p3X)==1 ){			//bit 2
		localFlag ^= 0x000004;
	}
	
	if( (p1X - p2X)==0 ){							//bit 3
		localFlag ^= 0x000008;
	}

	if( (p1X - p3X)==0 ){							//bit 4
		localFlag ^= 0x000010;
	}

	if( (p2X - p3X)==0 ){							//bit 5
		localFlag ^= 0x000020;
	}
	
	if( (p1Y - p2Y)==1 || (p2Y - p1Y)==1 ){			//bit 6
		localFlag ^= 0x000040;
	}

	if( (p1Y - p3Y)==1 || (p3Y - p1Y)==1 ){			//bit 7
		localFlag ^= 0x000080;
	}
	
	if( (p3Y - p2Y)==1 || (p2Y - p3Y)==1 ){			//bit 8
		localFlag ^= 0x000100;
	}
	
	if( (p1Y - p2Y)==0 ){							//bit 9
		localFlag ^= 0x000200;
	}

	if( (p1Y - p3Y)==0 ){							//bit 10
		localFlag ^= 0x000400;
	}

	if( (p2Y - p3Y)==0 ){							//bit 11
		localFlag ^= 0x000800;
	}
	
	// Condition of the Next point positions

	if( (p1Xn - p2Xn)==1 || (p2Xn - p1Xn)==1 ){		// bit 12
		localFlag ^= 0x001000;
	}

	if( (p1Xn - p3Xn)==1 || (p3Xn - p1Xn)==1 ){		//bit 13
		localFlag ^= 0x002000;
	}
	
	if( (p3Xn - p2Xn)==1 || (p2Xn - p3Xn)==1 ){		//bit 14
		localFlag ^= 0x004000;
	}
	
	if( (p1Xn - p2Xn)==0 ){							//bit 15
		localFlag ^= 0x008000;
	}

	if( (p1Xn - p3Xn)==0 ){							//bit 16
		localFlag ^= 0x010000;
	}

	if( (p2Xn - p3Xn)==0 ){							//bit 17
		localFlag ^= 0x020000;
	}
	
	if( (p1Yn - p2Yn)==1 || (p2Yn - p1Yn)==1 ){		//bit 18
		localFlag ^= 0x040000;
	}

	if( (p1Yn - p3Yn)==1 || (p3Yn - p1Yn)==1 ){		//bit 19
		localFlag ^= 0x080000;
	}
	
	if( (p3Yn - p2Yn)==1 || (p2Yn - p3Yn)==1 ){		//bit 20
		localFlag ^= 0x100000;
	}
	
	if( (p1Yn - p2Yn)==0 ){							//bit 21
		localFlag ^= 0x200000;
	}

	if( (p1Yn - p3Yn)==0 ){							//bit 22
		localFlag ^= 0x400000;
	}

	if( (p2Yn - p3Yn)==0 ){							//bit 23
		localFlag ^= 0x800000;
	}
	
	pointCNConditionFlag = localFlag;	// Save the flag
	
	localFlag = 0;			// Prepare to set pointMixedConditionFlag
	
	if(pointsNext[2] == 1){									// if point 1 moving this turn
		
		//p1Xn
		
		if( (p1Xn - p2X)==1 || (p2X - p1Xn)==1 ){			// bit 0 
			localFlag ^= 0x000001;
		}

		if( (p1Xn - p3X)==1 || (p3X - p1Xn)==1 ){			//bit 1
			localFlag ^= 0x000002;
		}
		
		if( (p1Xn - p2X)==0 ){								//bit 2
			localFlag ^= 0x000004;
		}
		
		if( (p1Xn - p3X)==0 ){								//bit 3
			localFlag ^= 0x000008;
		}

		//p1Yn
		
		if( (p1Yn - p2Y)==1 || (p2Y - p1Yn)==1 ){			//bit 4
			localFlag ^= 0x000010;
		}

		if( (p1Yn - p3Y)==1 || (p3Y - p1Yn)==1 ){			//bit 5
			localFlag ^= 0x000020;
		}
		
		if( (p1Yn - p2Y)==0 ){								//bit 6
			localFlag ^= 0x000040;
		}

		if( (p1Yn - p3Y)==0 ){								//bit 7
			localFlag ^= 0x000080;
		}
	}
	
	if(pointsNext[5] == 1){									// if point 2 moving this turn
		
		//p2Xn
		
		if( (p2Xn - p1X)==1 || (p1X - p2Xn)==1 ){			//bit 8
			localFlag ^= 0x000100;
		}
		
		if( (p2Xn - p3X)==1 || (p3X - p2Xn)==1 ){			//bit 9
			localFlag ^= 0x000200;
		}

		if( (p2Xn - p1X)==0 ){								//bit 10
			localFlag ^= 0x000400;
		}

		if( (p2Xn - p3X)==0 ){								//bit 11
			localFlag ^= 0x000800;
		}
		
		//p2Yn
		
		if( (p2Yn - p1Y)==1 || (p1Y - p2Yn)==1 ){			// bit 12
			localFlag ^= 0x001000;
		}

		if( (p2Yn - p3Y)==1 || (p3Y - p2Yn)==1 ){			//bit 13
			localFlag ^= 0x0020000;
		}
		
		if( (p2Yn - p1Y)==0 ){								//bit 14
			localFlag ^= 0x004000;
		}
		
		if( (p2Yn - p3Y)==0 ){								//bit 15
			localFlag ^= 0x008000;
	}

	}
	
	if(pointsNext[8] == 1){									// if point 3 moving this turn
		
		//p3Xn
		
		if( (p3Xn - p1X)==1 || (p1X - p3Xn)==1 ){			//bit 16
			localFlag ^= 0x010000;
		}

		if( (p3Xn - p2X)==1 || (p2X - p3Xn)==1 ){			//bit 17
			localFlag ^= 0x020000;
		}
		
		if( (p3Xn - p1X)==0 ){								//bit 18
			localFlag ^= 0x040000;
		}

		if( (p3Xn - p2X)==0 ){								//bit 19
			localFlag ^= 0x080000;
		}
		
		//p3Yn
		
		if( (p3Yn - p1Y)==1 || (p1Y - p3Yn)==1 ){			//bit 20
			localFlag ^= 0x100000;
		}
		
		if( (p3Yn - p2Y)==1 || (p2Y - p3Yn)==1 ){			//bit 21
			localFlag ^= 0x200000;
		}

		if( (p3Yn - p1Y)==0 ){								//bit 22
			localFlag ^= 0x400000;
		}

		if( (p3Yn - p2Y)==0 ){								//bit 23
			localFlag ^= 0x800000;
	}
	
	}

	pointMixedConditionFlag = localFlag;
	
}


//
//Sets the pointColisionFlag
//
void pointColisionFlagSet(void)
{
	register unsigned short locPointColisionFlag = 0;
	register unsigned int	locPointCNConditionFlag = pointCNConditionFlag;
	register unsigned int	locPointMixedConditionFlag = pointMixedConditionFlag;
	
	if(pointsNext[2] == 1){				//if point 1 moving this turn
		
		if( (locPointMixedConditionFlag & 0x44) == 0x44 ){			//P1 hits P2
			locPointColisionFlag ^= 0x0001;
		}
		
		if( (locPointMixedConditionFlag & 0x88) == 0x88 ){			//P1 hits P3
			locPointColisionFlag ^= 0x0002;
		}
		
		if( (locPointCNConditionFlag & 0x208000) == 0x208000 ){		//P1 and P2 want same spot
			locPointColisionFlag = 0x0040;
		}
		
		if( (locPointCNConditionFlag & 0x410000) == 0x410000 ){		//P1 and P3 want same spot
			locPointColisionFlag ^= 0x0080;
		}

	}
	
	if(pointsNext[5] == 1){				//if point 2 moving this turn
	
		if( (locPointMixedConditionFlag & 0x4400) == 0x4400 ){		//P2 hits P1
			locPointColisionFlag ^= 0x0004;
		}
		
		if( (locPointMixedConditionFlag & 0x8800) == 0x8800 ){		//P2 hits P3
			locPointColisionFlag ^= 0x0008;
		}
		
		if( (locPointCNConditionFlag & 0x820000) == 0x820000){		//P2 and P3 want same spot
			locPointColisionFlag = 0x0100;			
		}
	}
	
	if(pointsNext[8] == 1){				//if point 3 moving this turn
	
		if( (locPointMixedConditionFlag & 0x440000) == 0x440000 ){	//P3 hits P1
			locPointColisionFlag ^= 0x0010;
		}
		
		if( (locPointMixedConditionFlag & 0x880000) == 0x880000 ){	//P3 hits P2
			locPointColisionFlag ^= 0x0020;		
		}
	}

	pointColisionFlag = locPointColisionFlag;
}
